const glob = require('glob')
const { resolve } = require("path");
const { merge } = require('webpack-merge')
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const PurgeCSSWebpackPlugin = require('purgecss-webpack-plugin')
const TerserWebpackPlugin = require("terser-webpack-plugin");

const BaseConfig = require('./webpack.config.base')
const paths = require('./paths')
const { ROOT_PATH } = require("./constant");

const ProdWebpackConfig = merge(BaseConfig, {
  mode: "production",
  devtool: "source-map",
  // 使用文件缓存
  cache: { type: 'filesystem', buildDependencies: { config: [__filename] } },
  plugins: [
    // 将public下面的资源复制到dist目录去（除了index.html）
    new CopyWebpackPlugin({
      patterns: [
        {
          from: paths.public,
          to: paths.dist,
          toType: "dir",
          noErrorOnMissing: true, // 不生成错误
          globOptions: {
            ignore: ["**/index.html"], // 忽略文件
          },
          info: {
            minimized: true, // 跳过terser压缩js
          },
        }
      ],
      options: {
        concurrency: 100, // 并发数
      },
    }),
    // 抽离css为单独文件
    new MiniCssExtractPlugin({
      filename: "static/css/[name].[contenthash:10].css",
      chunkFilename: "static/css/[name].[contenthash:10].chunk.css",
      ignoreOrder: true,
    }),
    // 用于删除未使用的 css 的Webpack插件
    new PurgeCSSWebpackPlugin({
      paths: glob.sync(`${paths.src}/**/*`, { nodir: true }),
    })
  ],
  optimization: {
    minimize: true,
    // 压缩的操作
    minimizer: [
      // 压缩css 需要把mode设置为 生产环境 production
      new CssMinimizerPlugin(),
      // 通过TerserPlugin覆盖默认压缩工具 压缩js打包文件 优化build速度、优化start速度
      new TerserWebpackPlugin({
        extractComments: false, // 去除所有注释
        exclude: /node_modules/, // 排出掉node_modules文件，使star速度更快
        terserOptions: {
          compress: { pure_funcs: ["console.log"] }, // 去除所有console.log函数
        },
      }),
    ],
    // 代码分割
    splitChunks: {
      chunks: "all",
      cacheGroups: {
        // layouts通常是admin项目的主体布局组件，所有路由组件都要使用的可以单独打包，从而复用
        layouts: {
          name: "layouts",
          test: paths.layouts,
          priority: 40,
        },
        // 如果项目中使用antd，此时将所有node_modules打包在一起，那么打包输出文件会比较大。
        // 所以我们将node_modules中比较大的模块单独打包，从而并行加载速度更好
        antd: {
          name: "chunk-antd",
          test: /[\\/]node_modules[\\/]antd(.*)/,
          priority: 30,
        },
        // 将react相关的库单独打包，减少node_modules的chunk体积。
        react: {
          name: "react",
          test: /[\\/]node_modules[\\/]react(.*)?[\\/]/,
          priority: 20,
          chunks: "initial",
        },
        libs: {
          name: "chunk-libs",
          test: /[\\/]node_modules[\\/]/,
          priority: 10, // 权重最低，优先考虑前面内容
          chunks: "initial",
        },
      },
    },
    runtimeChunk: {
      name: (entrypoint) => `runtime~${entrypoint.name}`,
    }
  },

  performance: false, // 关闭性能分析，提示速度
})


module.exports = ProdWebpackConfig